import { Component, OnInit, OnChanges } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApisService } from '../../apis.service';
import * as moment from 'moment';

@Component({
  selector: 'super-list',
  templateUrl: './super-list.component.html',
  styleUrls: ['./super-list.component.css'],
  inputs: [
    "colInRow",
    "PageSize",
    "PageNo",
    "colClass",
    "rowClass",
    "showType",
    "query"
  ]
})
export class SuperListComponent implements OnInit {
  colInRow : number = 1;
  PageSize : number = 12;
  PageNo : number = 1;
  query : string = "";
  goods : any[] = [];
  colClass : string = "col-md-12";
  rowClass : string = "row";
  totalPages:number=0;
  lastPageNo:number =0;
  showType:number=0;

  constructor(private apiService:ApisService) { }

  dataLoad(){
    if(this.lastPageNo==this.PageNo){
      return;
    }
    this.lastPageNo=this.PageNo;
    this.goods=[];
    this
    .apiService.superCoupon({
      PageSize:this.PageSize,
      PageNo:this.PageNo,
      query:this.query
    })
    .then(result => {
      if(result.error){
        return;
      }

      this.totalPages = 100;
      let rowIdx = 0;
      let rows = [];
      result.forEach((item, idx) => {
        if (idx % this.colInRow == 0 && idx > 0) {
          this
            .goods
            .push(rows);
          rows = [];
        }
        if(item.coupon_info){
          var nums=item.coupon_info.match(/\d+(\.\d+)?/g);
          item.worth=nums[nums.length-1];
        }else{
          return;
        }
        item.real_price= item.zk_final_price-item.worth;
        item.real_price=Math.round(item.real_price*100)/100;
        
        rows.push(item);
      });
      if (rows.length > 0) {
        this
          .goods
          .push(rows);
      }
    });
  }
  ngOnChanges() {
    this.dataLoad();
  }
  ngOnInit() {
    this.dataLoad();
  }
}
